<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of fLanguageSourceArrayclass
 *
 * @author TrongHieu
 */
class fLanguageSourceArray implements fLanguageSourceInterface {
    private $_dir;
    protected $_culture;
    protected $_sources = array();
    
    public function  __construct($culture) {
        $this->_culture = $culture;
        $this->_dir = _LANGUAGES_PATH_.fFactory::getApplication()->getName()._DS_.$culture._DS_;
    }

    /**
	 * Translate
	 *
	 * @param string $string
	 * @return string
	 */
	public function translate($string) {
        if (isset($this->_sources[$string])) {
            return $this->_sources[$string];
        }

        return $string;
	}
    
    //put your code here
    public function import($namespace, $require = true) {
        $file = $this->_dir.$namespace.'.php';
        if (!file_exists($file)) {
			if ($require)
				throw new fException('File language: "'. $file .'" does not exists!');
			else
				return;
		}
        ob_start();
        $data = include($file);
        ob_end_clean();

        if(!is_array($data)) {
            throw new fException("Error including array or file '" . $file ."'");
        }

        $this->add($data);
    }
    
    /**
	 * Add source
	 *
	 * @param array $source
	 */
	public function add($source) {
		$source = (array) $source;
		$this->_sources = array_merge($this->_sources, $source);
	}

    /**
	 * Clear
	 *
	 * @param mixed $singular	.$singular = null, clear all.
	 */
	public function clear($singular = null) {
		if ($singular == null) {
			$this->_sources = array();
		}
		else {
			unset($this->_sources[$singular]);
		}
	}
}
